/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.misc;

import java.util.*;
import java.sql.*;

import nfse.*;

public class Indexer {

    public static void main(String[] args) {
        try {
            int[] types = NetFSE.getTypes();
            
            
            
            Connection c = DriverManager.getConnection("jdbc:mysql://localhost"  + "/",
                    NetFSE.DYNAMIC_DB_USER, NetFSE.DYNAMIC_DB_PASSWORD);
            Statement s = c.createStatement();
            for (int i = 0; i < types.length; i++) {
                NetFSERecord rec = NetFSE.getTypeClass(types[i]);
                String name = rec.getTypeName().toLowerCase();
                
                Class.forName("org.postgresql.Driver");
                String url = "jdbc:postgresql://localhost/" +name;
                Properties props = new Properties();
                props.setProperty("user", NetFSE.DYNAMIC_DB_USER);
                props.setProperty("password", NetFSE.DYNAMIC_DB_PASSWORD);
                props.setProperty("client_encoding", "UTF8");

                ResultSet rs = s.executeQuery("show tables");
                Vector temp = new Vector();
                while (rs.next()) {
                    temp.addElement(rs.getString(1));
                }
                rs.close();
                for (int x = 0; x < temp.size(); x++) {
                    String[] indexSQL = rec.getTempIndexSQL((String) temp.elementAt(x));
                    for (int y = 0; y < indexSQL.length; y++) {
                        try {
                            long ts = System.currentTimeMillis();
                            System.out.print(indexSQL[y] + "...");
                            s.execute(indexSQL[y]);
                            long ts2 = System.currentTimeMillis();
                            long ms = ts2 - ts;
                            float sec = (float) ms / (float) 1000;
                            System.out.println(sec + " seconds");
                        } catch (Exception e) {
                            System.out.println();
                            e.printStackTrace();
                        }
                    }
                }
            }
            s.close();
            c.close();

        } catch (Exception E) {
            E.printStackTrace();
        }
    }

}
